/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     SummerGift   first version
 * 2018-11-19     flybreak     add stm32f407-atk-explorer bsp
 */

#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "usermodbus.h"
#include "userport.h"
#include "uconfig.h"
#include "memory.h"
#include "shell.h"
#include "useradc.h"
#include "userled.h"
#include "user4g.h"
#include "userinput.h"
#include "userserial.h"

#define DBG_TAG "main"
#define DBG_LVL  DBG_ERROR
#include <rtdbg.h>

static rt_thread_t tid1 = RT_NULL;
rt_event_t EVENTDevic;

static int APP_TASK_INIT(void);
static void LED_TASK_INIT(void);
void Vled_task(void *parameter);

int APP_VERSION[3]={0,0,2};
int versions;
/****************************************************************************/
int main(void)
{		
  LOG_E("The current version of firmware is V%d.%d.%d\n",APP_VERSION[0],APP_VERSION[1],APP_VERSION[2]);
	versions=APP_VERSION[0]*100+APP_VERSION[1]*10+APP_VERSION[2];
	EVENTDevic=rt_event_create("Cevent", RT_IPC_FLAG_PRIO);	
  if(EVENTDevic==RT_NULL)
	{
		rt_kprintf("event create failed.\n");		  
	}
	rt_thread_mdelay(10000);	
	FileSystemInit();		
	lora_set_read();	
	sys_set_read();	
  idtab_set_read();	
  LED_TASK_INIT();	
  APP_TASK_INIT();
  MX_RTC_Init();
  
}
/****************************************************************************/
/****************************************************************************/	 
static void LED_TASK_INIT(void)
{
	tid1=rt_thread_create("led",Vled_task,RT_NULL,512,16,500);
	if(tid1!=RT_NULL)
		{ rt_thread_startup(tid1);
			tid1=RT_NULL;
		}
}

/****************************************************************************/
/****************************************************************************/	 
static void Vled_task(void *parameter)
{

	rt_uint32_t recvedEvent;
	char timet[50];
  LED_init();
	while (1)
	{
		  if(rt_event_recv(EVENTDevic,EVENT_SocketConnect|EVENT_SocketDisconnect,RT_EVENT_FLAG_OR, 0 ,&recvedEvent)>=0){
					switch(recvedEvent){
						 case EVENT_SocketConnect:
									rt_thread_mdelay(500);
									RUN_LED_OFF();
									rt_thread_mdelay(500);
									RUN_LED_ON();
									break;
						 case EVENT_SocketDisconnect:
									RUN_LED_ON();
									rt_thread_mdelay(2000);
									RUN_LED_OFF();
									rt_thread_mdelay(2000);
									break;										
						}
			}
			else{
						RUN_LED_ON();
						rt_thread_mdelay(2000);
						RUN_LED_OFF();
						rt_thread_mdelay(2000);
	    }
  }						
}

/****************************************************************************/
/****************************************************************************/	 
static int APP_TASK_INIT(void)
{
	tid1=rt_thread_create("send_4G",Vsend_4G_task,RT_NULL,2048,13,500);
	if(tid1!=RT_NULL)
	{ rt_thread_startup(tid1);
	}
	tid1=rt_thread_create("receive_4G",Vreceive_4G_task,RT_NULL,2048,14,500);
	if(tid1!=RT_NULL)
	{ rt_thread_startup(tid1);
	}
	tid1=rt_thread_create("serialTX",VserialTX_task,RT_NULL,4096,30,500);
	if(tid1!=RT_NULL)
	{ rt_thread_startup(tid1);
		tid1=0;
	}

	tid1=rt_thread_create("oilgun",Voilgun_task,RT_NULL,512,3,500);
	if(tid1!=RT_NULL)
	{ rt_thread_startup(tid1);
	}
#ifdef adc_mode_enable
	tid1=rt_thread_create("adc_get", adc_vol_sample_task, RT_NULL,1024, 15, 20);
	if(tid1!=RT_NULL)
	{ rt_thread_startup(tid1);
	}
#endif
#ifdef freemodbus_mode_enable
	tid1=rt_thread_create("modbuspoll",VmodbusPoll_task,RT_NULL,1024,17,500);
	if(tid1!=RT_NULL)
	{ rt_thread_startup(tid1);
		tid1=0;
	}
#endif
	return RT_EOK;
}

void reset4G()
{
	RESET_MODULE();
//	rt_pin_write(POWER_4G_PORT, PIN_LOW);
//	rt_thread_mdelay(2000);
//	rt_pin_write(POWER_4G_PORT, PIN_HIGH); 
}
	
MSH_CMD_EXPORT(reset4G, ......);


/*********************************end****************************************/
